home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / macgzip_03b2-src / macos / think / SPDCProg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-18  |  8.1 KB  |  340 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (C) 1993  SPDsoft
  3.  * 
  4.  */
  5.  
  6. #include "SPDProg.h"
  7. /*            SpinCursors by
  8.  *            America Online: LISPer
  9.  *             Internet: tree@uvm.edu
  10.  */
  11. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  12.  
  13. typedef struct                /* The structure of an 'acur' resource */
  14. {
  15.     short numberOfFrames;        /* number of cursors to animate */
  16.     short whichFrame;        /* current frame number */
  17.     CursHandle frame[1];        /* Pointer to the first cursor */
  18. } acur, *acurPtr, **acurHandle;
  19.  
  20. unsigned long int    SPDBackCycles;
  21.  
  22. Boolean        UseAnimatedCursor=false;
  23. Boolean        CanBreak=false;
  24. acurHandle    gFrameList;
  25.  
  26. Boolean InitAnimatedCursors(short acurID)
  27. {
  28.     register short i=0;
  29.     register short cursID;
  30.     Boolean noErrFlag = FALSE;
  31.     SPDBackCycles=0;
  32.     
  33.     if((gFrameList = (acurHandle) GetResource('acur',acurID)))
  34.     {
  35.         /* got it! */
  36.         noErrFlag = TRUE;
  37.         
  38.         while((i<(*gFrameList)->numberOfFrames) && noErrFlag)
  39.         {
  40.             /*
  41.              * The id of the cursor is stored in
  42.              * the high word of the frame handle
  43.              */
  44.             
  45.             cursID = (int) HiWrd((long) (*gFrameList)->frame[i]);
  46.             
  47.             (*gFrameList)->frame[i] = GetCursor(cursID);
  48.             
  49.             if((*gFrameList)->frame[i])
  50.                 i++;            /* get the next one */
  51.             else
  52.                 noErrFlag=FALSE;    /* foo! we couldn't find the cursor */
  53.         }
  54.     }
  55.     if(noErrFlag)
  56.     {
  57.         (*gFrameList)->whichFrame = 0;
  58.         UseAnimatedCursor=true;
  59.     }
  60.     return noErrFlag;
  61. }
  62.  
  63. /* Free up the storage used by the current animated cursor and all
  64.    of its frames */
  65. void ReleaseAnimatedCursors()
  66. {
  67.     int i;
  68.     
  69.     if(UseAnimatedCursor)
  70.     {
  71.         for(i=0;i<(*gFrameList)->numberOfFrames;i++)
  72.             ReleaseResource((Handle) (*gFrameList)->frame[i]);
  73.         ReleaseResource((Handle) gFrameList);
  74.     }
  75.     UseAnimatedCursor=false;
  76.     SetCursor(&qd.arrow);
  77.  
  78. }
  79.  
  80. /************************************************************************************/
  81.  
  82. /* Opens the MModal dialog. */
  83. /* Just for grins, I'm also saving and restoring it's position in*/
  84. /* a very simple way */
  85.  
  86. void DrawMovable(WindowPtr drawIt);
  87.  
  88. Boolean    UseMModalProg=false;
  89.  
  90. static Rect        barRect; 
  91. static Rect        greyRect;
  92.  
  93. static ControlHandle    ButtonHndl;
  94.  
  95. unsigned long int SPDEnd, SPDNow;
  96.  
  97. Str255    SPDPstr="\p";
  98. char*    SPDpstr=(char*)SPDPstr;
  99.  
  100. static    Boolean        gModalUp;
  101.     Boolean        gSavedPos;
  102.     Point        gSavedPoint;
  103.  
  104. #define    kMyModalKind    1000
  105.  
  106. void InitMovableModal(unsigned long int theEnd)
  107. {
  108.     WindowPtr    temp;
  109.     int        newWid, newLen, wid;
  110.     Rect    r;
  111.  
  112.  
  113.     temp = GetNewWindow(150, nil, (WindowPtr)-1);
  114.     /* install drawing proc */
  115.     SetWRefCon(temp, (long)DrawMovable);
  116.     
  117.     /* set the kind to my MModalwindow */
  118.     ((WindowPeek)temp)->windowKind = kMyModalKind;
  119.     gModalUp = true;
  120.     
  121.     if (gSavedPos) {
  122.         /* move it to the saved position */
  123.         /* move it to 0,0 to avoid any math at all */
  124.         MoveWindow(temp, 0, 0, false);
  125.         MoveWindow(temp, gSavedPoint.h, gSavedPoint.v, false);
  126.     }
  127.     
  128.     r = temp->portRect;
  129.     
  130.     SPDNow=0;
  131.     SPDEnd=(theEnd==0?1:theEnd);
  132.  
  133.     UseMModalProg=true;
  134.     
  135.         
  136.     CtoPstr(SPDpstr);
  137.     
  138.     wid = (r.right - r.left) - 40;
  139.     newWid = StringWidth(SPDPstr);
  140.     if (newWid > wid )
  141.     {
  142.         newLen = (int) SPDPstr[0];
  143.         wid = wid - CharWidth('…');
  144.         do
  145.         {
  146.             newWid = newWid - CharWidth(SPDPstr[newLen]);
  147.             newLen--;
  148.         }while((newWid > wid) && (SPDPstr[0] != 0x00));
  149.  
  150.         newLen ++;
  151.         SPDPstr[newLen] = '…';
  152.         SPDPstr[0] = (char)newLen;
  153.     }
  154.  
  155.  
  156.         
  157.     barRect.left =    r.left+20;
  158.     barRect.right =    r.right-20;
  159.     barRect.bottom =    r.bottom - 20;
  160.     barRect.top =    barRect.bottom-20;
  161.     
  162.     if(CanBreak)
  163.     {
  164.     
  165.     ButtonHndl = GetNewControl(128, temp);
  166.     
  167.     barRect.right  -= (20+ (*ButtonHndl)->contrlRect.right);
  168.     OffsetRect( &((*ButtonHndl)->contrlRect), barRect.right + 20, barRect.top );
  169.  
  170.     ShowControl(ButtonHndl);
  171.     
  172.     
  173.     }   
  174.  
  175.  
  176.     greyRect.left =    barRect.left+1;
  177.     greyRect.right =    greyRect.left;
  178.     greyRect.bottom =    barRect.bottom-1;
  179.     greyRect.top =    barRect.top+1;
  180.     
  181.     ShowWindow(temp);
  182.     DrawMovable(temp);
  183.  
  184.     SetPort(temp);
  185. }
  186.  
  187. void ReleaseMovableModal(void)
  188. {
  189.     WindowPtr temp = FrontWindow();
  190.  
  191.     
  192.     
  193.      if(UseMModalProg)
  194.     {
  195.         if(CanBreak)
  196.         {
  197.         DisposeControl(ButtonHndl);
  198.         }
  199.  
  200.         /* the front window really should be my modal window */
  201.         /* if it isn't, I'm bailing.*/
  202.         gSavedPos = true;
  203.         gSavedPoint.h = temp->portRect.left;
  204.         gSavedPoint.v = temp->portRect.top;
  205.         LocalToGlobal(&gSavedPoint);
  206.         if (((WindowPeek)temp)->windowKind == kMyModalKind)
  207.             CloseWindow(temp);
  208.     
  209.         gModalUp = false;
  210.         UseMModalProg=false;
  211.     }
  212.     
  213.     
  214. }
  215.  
  216. static void DrawMovable(WindowPtr drawIt)
  217. {
  218.     WindowPtr tempWP;
  219.     unsigned long int wbar;
  220.  
  221.  
  222.     BeginUpdate(drawIt);
  223.     GetPort(&tempWP);
  224.     SetPort(drawIt);
  225.     
  226.         MoveTo(20,20);        /* h, v */
  227.         DrawString(SPDPstr);
  228.  
  229.     wbar = ((float)SPDNow/SPDEnd)*(barRect.right-barRect.left) + barRect.left; 
  230.     if ( wbar >= barRect.right ) wbar = barRect.right;
  231.     FrameRect(&barRect);
  232.     greyRect.right=wbar-1;
  233.     
  234. #if defined(__MWERKS__)
  235.     FillRect( &greyRect, &qd.gray );
  236. #else
  237.     FillRect( &greyRect, qd.gray );
  238. #endif
  239.  
  240.     DrawControls(drawIt);
  241.     EndUpdate(drawIt);
  242.     SetPort(tempWP);
  243. }
  244.  
  245. /************************************************************************************/
  246.  
  247. Boolean    SPDSystemTask()
  248. {
  249.     typedef long (*MyProcPtr)();
  250.     
  251.     EventRecord    theEvent;
  252.     MyProcPtr    drawProc;
  253.     WindowPtr    twindow;
  254.     static unsigned long int TheLastTime;
  255.     ControlHandle    WhichCtl;
  256.     
  257.     if(UseMModalProg)
  258.     {
  259.         InvalRect(&barRect);
  260.     }
  261.     while(GetNextEvent(everyEvent, &theEvent))
  262.     {
  263.     if ((theEvent.what == keyDown)&&(CanBreak))
  264.     {
  265.         if (((theEvent.modifiers & cmdKey) != 0)
  266.         && ((theEvent.message & charCodeMask) == '.' ))
  267.         {
  268.             SetCursor(&qd.arrow);
  269.             return(true);
  270.         }
  271.     }else if ((theEvent.what == updateEvt)&&(UseMModalProg))
  272.     {
  273.                 /* Make sure it's my window before I jump through the refCon */
  274.                 /* Why, since DA's have they're own layer in 7.0? */
  275.                 /* BECAUSE there are other people in the universe who will */
  276.                 /* add things to your windowList.BalloonWriter, for example, */
  277.                 /* so you still need to be careful */
  278.                 if (((WindowPeek)theEvent.message)->windowKind == kMyModalKind)
  279.                 {
  280.                     /* get the drawing proc from the refCon */
  281.                     drawProc = (MyProcPtr)GetWRefCon((WindowPtr)theEvent.message);
  282.                     /* jump to it */
  283.  
  284.                     drawProc((WindowPtr)theEvent.message);
  285.  
  286.                 }
  287.         }else if (theEvent.what == mouseDown)
  288.         {
  289.             switch (FindWindow(theEvent.where, &twindow))
  290.             {
  291.                 case inSysWindow:
  292.                                 /* pass to the system */
  293.                                 SystemClick(&theEvent, twindow);
  294.                                 break;
  295.                         case inDrag:
  296.                             if (twindow != FrontWindow() && gModalUp)
  297.                             {
  298.                             /* don't do anything, can't drag a back window */
  299.                                SysBeep(1);
  300.                             } else
  301.                             {
  302.                             DragWindow(twindow, theEvent.where, &qd.screenBits.bounds);
  303.                             }
  304.                             break;
  305.                         
  306.                         case inContent:
  307.                         
  308.                                GlobalToLocal(&theEvent.where);
  309.                                
  310.                                if(inButton == FindControl(theEvent.where, twindow, &WhichCtl))
  311.                                {
  312.                                    if(0!=TrackControl(WhichCtl, theEvent.where, nil))
  313.                                    {
  314.                                      SetCursor(&qd.arrow);
  315.                                      return(true);
  316.                                  }
  317.                             }            
  318.                             break;
  319.                            
  320.                         
  321.                         default:
  322.                                                                
  323.                             break;
  324.  
  325.                 
  326.             }
  327.     }
  328.     
  329.     if(( theEvent.when-TheLastTime > kTicksCursor )&&(UseAnimatedCursor))
  330.     {
  331.         SetCursor(*((*gFrameList)->frame[(*gFrameList)->whichFrame++]));
  332.         if((*gFrameList)->whichFrame == (*gFrameList)->numberOfFrames)
  333.             (*gFrameList)->whichFrame = 0;
  334.             
  335.         TheLastTime=theEvent.when;
  336.         /* This will be the last time... */
  337.     }
  338.     }
  339.     return(false);
  340. }